package leetcode.stack_queue;

import java.util.ArrayDeque;

public class Lc2390 {

    //栈实现
    public String removeStars(String s) {
        ArrayDeque<Character> deque = new ArrayDeque();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '*') {
                deque.pollLast();
            } else deque.offerLast(s.charAt(i));
        }
        StringBuilder builder = new StringBuilder();
        while (!deque.isEmpty())
            builder.append(deque.pollFirst());
        return builder.toString();
    }



    //方法二 模拟，效率低
    public String removeStars2(String s) {
        int[] preCharIndex = new int[s.length()];
        StringBuilder builder = new StringBuilder(s);
        for (int i = 0; i < s.length(); i++) {
            if (builder.charAt(i) != '*') {
                preCharIndex[i] = i;
            } else {
                builder.replace(i, i+1, "-");
                int preCharId = preCharIndex[i-1];
                builder.replace(preCharId, preCharId + 1, "-");
                preCharIndex[preCharId] = preCharId == 0 ? -1 : preCharIndex[preCharId - 1];
                preCharIndex[i] = preCharIndex[preCharId];
            }
        }
        return builder.toString().replace("-","");
    }

    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder("absbe");
        builder.replace(0,1, "A");
        System.out.println(builder.toString());
        String s = "----";
        System.out.println(s.replace("-",""));
    }

}
